package com.itheima.reggie_auto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_auto.entity.Employee;
import com.itheima.reggie_auto.entity.RePassVO;
import com.itheima.reggie_auto.exception.CustomException;
import com.itheima.reggie_auto.mapper.EmployeeMapper;
import com.itheima.reggie_auto.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 按账号 username 查询
     * @param username
     * @return
     */
    @Override
    public Employee getOne(String username) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,username);
        return employeeMapper.selectOne(lqw);
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @Override
    public void save(Employee employee) {
        //处理前台数据
        String password = "123456";
        //对密码进行Md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //设置密码
        employee.setPassword(password);
        //设置默认状态--启用
        employee.setStatus(1);
        employeeMapper.insert(employee);

    }

    /**
     * 分页带条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page selectByPage(int page, int pageSize, String name) {
        //构造分页查询器
        Page pageInfo = new Page(page,pageSize);
        //构造条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //模糊查询 name
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //按修改时间排序
        lqw.orderByDesc(Employee::getUpdateTime);
        //分页查询
        employeeMapper.selectPage(pageInfo,lqw);
        return pageInfo;
    }

    @Override
    public void update(Employee employee) {
       employeeMapper.updateById(employee);
    }

    @Override
    public Employee getById(Long id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public void updatePwd(Long empId, RePassVO rePassVO) {
        //加密的原始密码
        String password = DigestUtils.md5DigestAsHex(rePassVO.getPassword().getBytes());
        String newPwd1 = DigestUtils.md5DigestAsHex(rePassVO.getNewPassword().getBytes());
        String newPwd2 = DigestUtils.md5DigestAsHex(rePassVO.getNewPassword2().getBytes());

        //新旧密码不能一致
        if (password.equals(newPwd1)) {
            throw new CustomException("新旧密码不能一致");
        }
        //在比较两次输入是否一致
        if (!newPwd1.equals(newPwd2)) {
            throw new CustomException("两次输入的密码不一致");
        }

        if (employeeMapper.selectById(empId).getPassword().equals(password)) {
            //相等
                //一致则修改
                Employee employee = new Employee();
                employee.setId(empId);
                employee.setPassword(newPwd1);
                employee.setUpdateTime(LocalDateTime.now());
        }else{
            //旧密码错误
            throw new CustomException("旧密码错误");
        }

    }


}
